"use strict";
window.onload = function () {
	new lineCanvas({
		//绘制canvas的父级div
		el: document.getElementById("canvas"),
		//清除按钮
		clearEl: document.getElementById("clearCanvas"),
		//保存按钮
		saveEl: document.getElementById("saveCanvas"),
		//线条粗细，选填
		linewidth: 2,
		//线条颜色，选填
		color: "black",
		//线条背景，选填
		background: "#D8D8D8"

	});
};

var wt = {};
var flag = false;

function lineCanvas(obj) {
	this.background = "#ffffff";
	for (var i in obj) {
		this[i] = obj[i];
	};
	this.canvas = document.createElement("canvas");
	this.el.appendChild(this.canvas);
	var l = this.canvas.offsetLeft;//canvas左边距
	var t = this.canvas.offsetTop;//canvas上边距
	this.cxt = this.canvas.getContext("2d");
	this.canvas.width = this.el.clientWidth - 20;
	this.canvas.height = this.el.clientHeight;
	this.cxt.fillStyle = this.background;
	this.cxt.strokeStyle = this.color;
	this.cxt.lineWidth = this.linewidth;
	this.cxt.lineCap = "round";

	//开始绘制
	this.canvas.addEventListener("touchstart", function (e) {
		this.cxt.beginPath();
		this.cxt.moveTo(e.changedTouches[0].pageX - 10, e.changedTouches[0].pageY - t);
	}.bind(this), false);

	//绘制中
	this.canvas.addEventListener("touchmove", function (e) {
		this.cxt.lineTo(e.changedTouches[0].pageX - 10, e.changedTouches[0].pageY - t);
		this.cxt.stroke();
		console.clear();
		e.preventDefault();
	}.bind(this), false);

	//结束绘制
	this.canvas.addEventListener("touchend", function () {
		this.cxt.closePath();
		console.clear();
		flag = true;
	}.bind(this), false);

	//清除画布
	this.clearEl.addEventListener("click", function () {
		this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
		flag = false;
	}.bind(this), false);

	//保存图片，直接转base64
	this.saveEl.addEventListener("click", function () {
		var imgBase64 = this.canvas.toDataURL();
		var filename = new Date().getTime() + "_" + (Math.random() * 100000).toFixed(0);
		if (!flag) {
			mui.alert('请先签名');
			return;
		}
		// imgBase64 调接口转图片url
		post("api/upload/base64", {
			file: imgBase64,
			filename: filename,
			dirPath: 'signImg'
		}).then(function (res) {
			if (res.data && res.data.url) {
				var sign_img = res.data.url;
				// 调用经办人签名接口
				post("api/enps/sign", {
					sign_img: sign_img,
					type: 1 //签约类型 1 个人 2 企业
				}).then(function (res) {
					if (res.data) {
						if (res.data.pay_status == 0) {//未支付
							if (res.data.is_local == true) {//是线下支付 -> 待支付页面
								setTimeout(function () {
									if (window != top) {
										top.location.href = "wait_pay.html"
									}
								}, 2500)
							}
						} else {//已支付
							if (res.data.status == 5) {//礼包待领取 -> 签约成功领取礼包页
								setTimeout(function () {
									if (window != top) {
										top.location.href = "sign_successful.html"
									}
								}, 2500)
							} else if (res.data.status == 6) {//待审核 -> 待审核页
								setTimeout(function () {
									if (window != top) {
										top.location.href = "wait_audit.html"
									}
								}, 2500)
							} else if (res.data.status == 7) {//审核不通过 -> 审核不通过页
								setTimeout(function () {
									if (window != top) {
										top.location.href = "audit_btg.html"
									}
								}, 2500)
							}
						}
					}
				})
			}
		})
	}.bind(this), false);
}
/**
 * 禁止浏览器下拉回弹
 */
function stopDrop() {
	var lastY; //最后一次y坐标点
	$(document.body).on('touchstart', function (event) {
		lastY = event.originalEvent.changedTouches[0].clientY; //点击屏幕时记录最后一次Y度坐标。
	});
	$(document.body).on('touchmove', function (event) {
		var y = event.originalEvent.changedTouches[0].clientY;
		var st = $(this).scrollTop(); //滚动条高度  
		if (y >= lastY && st <= 10) {
			//如果滚动条高度小于0，可以理解为到顶了，且是下拉情况下，阻止touchmove事件。
			lastY = y;
			event.preventDefault();
		}
		lastY = y;
	});
}
function parentFn() {
	parent.window.fatherMethod();
}